package org.jeecg.config.interceptor;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.annotation.Interceptor;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 日志拦截、
 * @author 3hmzz
 */
@Component
@Interceptor
public class LogInterceptor extends HandlerInterceptorAdapter {
	

    private long stime = 0;
    private long etime = 0;

    @Resource
    public Environment env;
    @Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		
		//请求时间
        request.setAttribute("requestStartTime", System.currentTimeMillis());
        
        stime = System.currentTimeMillis();

        return super.preHandle(request, response, handler);
	}

	
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
			throws Exception {
		if(modelAndView!=null){
			request.setAttribute("requestUi", modelAndView.getViewName()); 
		}
        etime = System.currentTimeMillis();
	}

	
	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
//        ResourceHttpRequestHandler  HttpRequestHandler
        if(handler instanceof HandlerMethod){
            //打印请求日志
            String methodName = ((HandlerMethod) handler).getMethod().getName();
            Class<?> methodClass = ((HandlerMethod) handler).getBean().getClass();

            StringBuffer sb=new StringBuffer();
            sb.append("控制器：").append(methodClass).append(",");//类名
            sb.append("方法名：").append(methodName).append(",");//类名
            System.out.println("==================控制器类："+ methodClass);
            System.out.println("==================方法名："+ methodName);
            if(request.getAttribute("requestUi")!=null){
                if((request.getAttribute("requestUi")+"").contains(".")){
                    System.out.println("==================ui文件："+request.getAttribute("requestUi"));
                }else{
                    System.out.println("==================ui文件："+request.getAttribute("requestUi")+".jsp");
                }

            }
            //运行日志记录
            long startTime = (Long) request.getAttribute("requestStartTime");
            long endtime = System.currentTimeMillis();
            String username="";

            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if(sysUser!=null){
                username = sysUser.getUsername();
                System.out.println("==================登录账号："+username);
            }
            String url =  request.getRequestURL().toString();
            System.out.println("==================请求地址："+url);
            System.out.println("==================执行时间"+(endtime-startTime)/1000+"秒\n\n");
        }

		super.afterCompletion(request, response, handler, ex);
		
	}

	

}